% % File		:	verity/ppg_0_1_display
% % Description	:   for Heart Rate Algorithm
% % Fuction		:	PPG signal display
% % Author		:	@hiyangdong
% % Version		:	V0.1
% % Time		:	12 Jan. 2016

%{
 1.0.1 read data and orignal calculate
%}
clear all
orignal_txt = textread('PAH0310_1217.txt','%s');    %#ok<DTXTRD> %read txt file data 32bit with 4byte and Low byte first
orignal_dec = hex2dec(orignal_txt);                 %hex 2 dec every byte
orignal_num = length(orignal_dec);                  %length of the orignal data
ppg_data_byte = 20;
ppg_size  = orignal_num/ppg_data_byte;               %length of the PPG data
ppg_data = zeros(1,ppg_size);                        %generate an 1-by-length empty array

%{
1.0.2 convert orignal hexadecimal data
data format first frame:
1 byte address
2 bytes PPG orignal data
2 bytes Gyro X
2 bytes Gyro Y
2 bytes Gyro Z
2 bytes Acce X
2 bytes Acce Y
2 bytes Acce Z

data format second frame:
1 byte address
2 bytes PPG orignal data
2 bytes Gyro X
2 bytes Gyro Y
2 bytes Gyro Z
2 bytes Magn X
2 bytes Magn Y
2 bytes Magn Z
2 bytes Barometer Z
2 bytes Temprature
%}

j=1;
for i=1:ppg_data_byte:orignal_num
    head_data = orignal_dec(i);
    j=j+1;
end

j=1;
for i=1:ppg_data_byte:orignal_num
    ppg_data(j)=orignal_dec(i+1)*256 + orignal_dec(i+2);
    j=j+1;
end

j=1;
m = 3;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    gyro_x_data(j) = temp_data;%typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 5;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    gyro_y_data(j) = temp_data;%typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 7;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    gyro_z_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 9;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    acce_x_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 11;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    acce_y_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 13;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    acce_z_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 15;
for i=1:ppg_data_byte:orignal_num
    magn_x_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');
    j=j+1;
end

j=1;
m = 16;
for i=1:ppg_data_byte:orignal_num
    magn_y_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');
    j=j+1;
end

j=1;
m = 17;
for i=1:ppg_data_byte:orignal_num
    magn_z_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');
    j=j+1;
end

j=1;
m = 18;
for i=1:ppg_data_byte:orignal_num
    baro_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');%orignal_dec(i+m);
    j=j+1;
end

j=1;
m = 16;
for i=1:ppg_data_byte:orignal_num
    temp_data(j) = single((orignal_dec(i+m)+100)/10.0);
    j=j+1;
end

ppg_size = length(ppg_data);
sampletime = 0.01;                               %sample time that unit is second
t = 0:sampletime:(ppg_size-1) * sampletime;     %time point

figure(1);clf();subplot(2,1,1);
plot(t,ppg_data,'k');
title('Time domain orignal PPG data');

samplefreq = 1/sampletime;                        %sample frequency that unit is Hz
f = (0:(ppg_size-1))/ppg_size*samplefreq;           %frequency point
fft_data=fft(ppg_data);
fft_data_abs=abs(fft_data);

figure(1);subplot(2,1,2);
plot(f,fft_data_abs,'k');
title('Frequency domain orignal PPG data ');

%{
1.0.9 plot other data
%}
figure(2);
plot_data = zeros(6,ppg_size);
plot_data(1,:) = gyro_x_data;
plot_data(2,:) = gyro_y_data;
plot_data(3,:) = gyro_z_data;
plot_data(4,:) = acce_x_data;
plot_data(5,:) = acce_y_data;
plot_data(6,:) = acce_z_data;

for i = 1:6
    subplot(2,3,i);
    plot(t,plot_data(i,:),'k');
end

% % 1.0.1  heart beat calculate
figure(3);
plot_data(1,:) = magn_x_data;
plot_data(2,:) = magn_y_data;
plot_data(3,:) = magn_z_data;
plot_data(4,:) = baro_data;

for i = 1:4
    subplot(2,2,i);
    plot(t,plot_data(i,:),'k');
end

disp('end');
